#include "CurrentWindow.h"

#include <QtGui>

CurrentWindow::CurrentWindow(NewParameters tempParams)
{
  QDoubleValidator *sci =  new QDoubleValidator();

  pulseType = new QComboBox();
  pulseType->addItem("Constant current");
  pulseType->addItem("Square Pulse");
  pulseType->addItem("Double Square Pulse");

  //constantAmplitude = new QLineEdit();
  //constantAmplitude->setValidator(valid);
  amplitude = new QLineEdit();
  amplitude->setValidator(sci);
  amplitude->setToolTip("current amplitude in Amps");
  //amplitude->setSuffix("mA");
  length = new QLineEdit();
  length->setValidator(sci);
  length->setToolTip("pulse length in seconds");
  //length->setSuffix("ns");

  QFormLayout *currentLayout = new QFormLayout();
  currentLayout->addRow("I=",amplitude);
  QFormLayout *lengthLayout = new QFormLayout();
  lengthLayout->addRow("length=",length);
  lengthWidget = new QWidget();
  lengthWidget->setLayout(lengthLayout);
  QVBoxLayout *squareLayout = new QVBoxLayout();
  squareLayout->addLayout(currentLayout);
  squareLayout->addWidget(lengthWidget);
  QWidget *squareWidget = new QWidget();
  squareWidget->setLayout(squareLayout);
  lengthWidget->hide();

  QWidget *doubleSquareWidget = new QWidget();
  QFormLayout *doubleSquareLayout = new QFormLayout();
  length1 = new QLineEdit();
  length1->setValidator(sci);
  length1->setToolTip("first pulse length in seconds");
  length2 = new QLineEdit();
  length2->setToolTip("second pulse length in seconds");
  length2->setValidator(sci);
  value1 = new QLineEdit();
  value1->setValidator(sci);
  value1->setToolTip("first pulse amplitude in Amps");
  value2 = new QLineEdit();
  value2->setValidator(sci);
  value2->setToolTip("second pulse amplitude in Amps");
  delay = new QLineEdit();
  delay->setValidator(sci);
  delay->setToolTip("delay between the two pulses in seconds");
  doubleSquareLayout->addRow("First Pulse Amplitude =",value1);
  doubleSquareLayout->addRow("First Pulse Length =",length1);
  doubleSquareLayout->addRow("Second Pulse Amplitude =",value2);
  doubleSquareLayout->addRow("Second Pulse Length =",length2);
  doubleSquareLayout->addRow("delay =",delay);
  doubleSquareWidget->setLayout(doubleSquareLayout);

  QWidget *phdWidget = new QWidget();
  maxAmp = new QLineEdit();
  maxAmp->setValidator(sci);
  minAmp = new QLineEdit();
  minAmp->setValidator(sci);
  stepAmp = new QLineEdit();
  stepAmp->setValidator(sci);
  maxLength = new QLineEdit();
  maxLength->setValidator(sci);
  minLength = new QLineEdit();
  minLength->setValidator(sci);
  stepLength = new QLineEdit();
  stepLength->setValidator(sci);
  QFormLayout *phdLayout = new QFormLayout();
  phdLayout->addRow("Imin",minAmp);
  phdLayout->addRow("Imax",maxAmp);
  phdLayout->addRow("Istep",stepAmp);
  QFormLayout *phdItLayout = new QFormLayout();
  phdItLayout->addRow("min length",minLength);
  phdItLayout->addRow("max length",maxLength);
  phdItLayout->addRow("length step",stepLength);
  phdItWidget = new QWidget();
  phdItWidget->setLayout(phdItLayout);
  QVBoxLayout *phdBox = new QVBoxLayout();
  phdBox->addLayout(phdLayout);
  phdBox->addWidget(phdItWidget);
  phdWidget->setLayout(phdBox);
  layout = new QStackedWidget();
  layout->addWidget(squareWidget);
  layout->addWidget(doubleSquareWidget);
  layout->addWidget(phdWidget);

  QVBoxLayout *mainLayout = new QVBoxLayout(this);
  mainLayout->addWidget(pulseType); 
  mainLayout->addWidget(layout);

  QObject::connect(pulseType,SIGNAL(activated(int)),this,SLOT(displayLayout(int))); 
}

CurrentWindow::~CurrentWindow()
{

}

void CurrentWindow::displayLayout(int choice)
{
  //QTextStream cout(stdout,QIODevice::WriteOnly);
  //cout<<pulseType->currentIndex()<<endl;
  switch(choice)
    {
    case 0 :
      {
	layout->setCurrentIndex(0);
	lengthWidget->hide();
	break;
      }
    case 1 :
      {
	layout->setCurrentIndex(0);
	lengthWidget->show();
	break;
      }
    case 2 :
      {
	layout->setCurrentIndex(1);
	break;
      }
    }
}

NewParameters CurrentWindow::getParameters(NewParameters params)
{
  QMap<QString,double> currentParams,squareParams,doubleParams;
  currentParams["value"]=amplitude->text().toFloat();
  currentParams["max"]=maxAmp->text().toFloat();
  currentParams["min"]=minAmp->text().toFloat();
  currentParams["step"]=stepAmp->text().toFloat();
  params.setCurrent(currentParams);
  squareParams["length"] = length->text().toFloat();
  squareParams["min"] = minLength->text().toFloat();
  squareParams["max"] = maxLength->text().toFloat();
  squareParams["step"] = stepLength->text().toFloat();
  params.setCurrentPulse(squareParams);
  doubleParams["firstAmplitude"] = value1->text().toFloat();
  doubleParams["secondAmplitude"] = value2->text().toFloat();
  doubleParams["firstLength"] = length1->text().toFloat();
  doubleParams["secondLength"] = length2->text().toFloat();
  doubleParams["delay"] = delay->text().toFloat();
  params.setDoublePulse(doubleParams);
  params.setCurrentPulseType(setPulseType());
  return params;
}

void CurrentWindow::loadParameters(NewParameters params)
{
  QMap<QString,double> currentParams,squareParams,doubleParams;
  QString str;
  currentParams = params.getCurrent();
  amplitude->clear();
  maxAmp->clear();
  minAmp->clear();
  stepAmp->clear();
  amplitude->insert(str.setNum(currentParams["value"]));
  maxAmp->insert(str.setNum(currentParams["max"]));
  minAmp->insert(str.setNum(currentParams["min"]));
  stepAmp->insert(str.setNum(currentParams["step"]));
 
  squareParams = params.getCurrentPulse();
  length->clear(); 
  minLength->clear();
  maxLength->clear();
  stepLength->clear();
  length->insert(str.setNum(squareParams["length"]));
  minLength->insert(str.setNum(squareParams["min"]));
  maxLength->insert(str.setNum(squareParams["max"]));
  stepLength->insert(str.setNum(squareParams["step"]));

  doubleParams = params.getDoublePulse();
  value1->clear();
  value2->clear();
  length1->clear();
  length2->clear();
  delay->clear();
  value1->insert(str.setNum(doubleParams["firstAmplitude"]));
  value2->insert(str.setNum(doubleParams["secondAmplitude"]));
  length1->insert(str.setNum(doubleParams["firstLength"]));
  length2->insert(str.setNum(doubleParams["secondLength"]));
  delay->insert(str.setNum(doubleParams["delay"]));
}

void CurrentWindow::showILoop()
{
  QTextStream cout(stdout,QIODevice::WriteOnly);
  cout<<"show current loop"<<endl;
  phdItWidget->hide();
  layout->setCurrentIndex(2);
}

void CurrentWindow::showLengthLoop()
{
  QTextStream cout(stdout,QIODevice::WriteOnly);
  cout<<"show current length loop"<<endl;
  phdItWidget->show();
  layout->setCurrentIndex(2);
}

void CurrentWindow::showTraj()
{
  QTextStream cout(stdout,QIODevice::WriteOnly);
  cout<<"show current traj"<<endl;
  layout->setCurrentIndex(0);
}

QString CurrentWindow::setPulseType()
{
  switch(pulseType->currentIndex())
    {
    case 0 :
      {
	return "nopulse";
      }
    case 1 :
      {
	return "square";
      }
    case 2 :
      {
	return "doublesquare";
      }
    }

  return "error";
}
